package com.web.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import com.web.constant.Constant;
import com.web.domain.Category;
import com.web.domain.Product;
import com.web.service.AdProductService;
import com.web.utils.BeanFactory;
import com.web.utils.UUIDUtils;
import com.web.utils.UploadUtils;

/**
 * Servlet implementation class UpdateProductServlet
 */
@WebServlet("/updateproduct") 
public class UpdateProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Map<String, Object> map = new HashMap<>();
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload up = new ServletFileUpload(factory);
			List<FileItem> list = up.parseRequest(request);
			String pimage = null;
			for (FileItem fi : list) {
				String fileItem = fi.getFieldName();
				if (fileItem.equals("pimage")) {
					pimage = fi.getString("utf-8");
				}
			}
			if (pimage != null && !pimage.equals("")) {
				for (FileItem fi : list) {
					String fileItem = fi.getFieldName();
					if (fi.isFormField()) {
						map.put(fileItem, fi.getString("utf-8"));
					} else {
						String name = fi.getName();
						String realName = UploadUtils.getRealName(name);
						InputStream is = fi.getInputStream();
						String uuidName = UploadUtils.getUUIDName(realName);

						String productReal = getServletContext().getRealPath("/products");
						// 创建随机目录

						FileOutputStream os = new FileOutputStream(new File(productReal, uuidName));
						IOUtils.copy(is, os);

						os.close();
						is.close();

						fi.delete();

						map.put(fileItem, "products/" + uuidName);
					}
				}
			} else {
				for (FileItem fi : list) {
					String fileItem = fi.getFieldName();
					if (fi.isFormField()) {
						map.put(fileItem, fi.getString("utf-8"));
					}
				}
				
			}
			// 封装Product对象
			Product product = new Product();
			BeanUtils.populate(product, map);
			AdProductService aps = (AdProductService) BeanFactory.getBean("AdProductService");
			aps.upProduct(product);

			response.sendRedirect(request.getContextPath() + "/adproduct?method=findByAdPage&pageNumber="+map.get("pageNumber"));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("更改商品失败!");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
